<template>
  <div class="right">
     <Breadcrumb
      :params1="'权限管理'"
      :params2="'角色列表'"
    ></Breadcrumb>
    <el-card class="box-card" >
      <el-table :data="list" border >
        <el-table-column type="expand" width="80">
          <template slot-scope="scope">
            <el-row
              class="first"
              v-for="(item1, index1) in scope.row.children"
              :key="index1"
            >
              <el-col :span="6">
                <el-tag
                  closable
                  @close="rightdel(scope.row.id, item1.id, scope.row)"
                >
                  {{ item1.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="18">
                <el-row
                  class="second"
                  v-for="(item2, index2) in item1.children"
                  :key="index2"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      type="success"
                      @close="rightdel(scope.row.id, item2.id, scope.row)"
                    >
                      {{ item2.authName }}
                    </el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      v-for="(item3, index3) in item2.children"
                      :key="index3"
                      type="warning"
                      @close="rightdel(scope.row.id, item3.id, scope.row)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="#" width="80"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="auto">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width="auto">
        </el-table-column>
        <el-table-column label="操作" width="350">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="info"
              icon="el-icon-setting"
              @click="distributionrights(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-tree
        ref='tree'
        :data="rihgtsdata"
        default-expand-all
        show-checkbox
        node-key="id"
        :default-expanded-keys="[2, 3]"
        :default-checked-keys="default_check"
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="assignment"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getroles_api, rolesdel, rolesrights, addrolesrights } from "@/api/index";
export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      rihgtsdata: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
      default_check: [],
      rolesId : ''
    };
  },
  methods: {
    async getRoles() {
      var res = await getroles_api();
      // console.log(res)

      this.list = res.data.data;
    },
    rightdel(roleId, rightId, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          var { data: res } = await rolesdel({ roleId, rightId });
          // console.log(res);
          if (res.meta.status == 200) {
            this.$message({
              type: "success",
              message: res.meta.msg,
            });
            row.children = res.data;
          } else {
            this.$message.error(res.meta.msg);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    async distributionrights(row) {
      this.rolesId = row.id
      this.dialogVisible = true;
      this.default_check = [];
      var res = await rolesrights()
      // console.log(res);
      this.rihgtsdata = res.data.data
      // console.log(row);
      this.recursion(row, this.default_check);
    },

    // 递归拿到最后子节点的 id

    recursion(obj, arr) {
      if (!obj.children) {
        return  arr.push(obj.id);
      } else {
        obj.children.forEach((item) => {
         
          this.recursion(item, arr);
        });
      }
    },

    //  分配权限的确认按钮来实现权限更新
   async assignment(){
      // console.log(this.rolesId);
      
     
      var rids = [...this.$refs.tree.getHalfCheckedKeys() , ...this.$refs.tree.getCheckedKeys()].join()

     var {data: res} = await addrolesrights({roleId :this.rolesId  , rids})

    //  console.log(res);
     
      if (res.meta.status == 200) {
            this.$message({
              type: "success",
              message: res.meta.msg,
            });
           this.dialogVisible = false
           this.getRoles()
          } else {
            this.$message.error(res.meta.msg);
          }

    },


    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },

  mounted() {
    this.getRoles();
  },
};
</script>

<style scoped lang='less'>
.box-card{
  width: 95%;
  margin: 0 auto;
  }
.first {
  padding: 20px 0;
  border-bottom: 1px solid #ccc;
  &:nth-child(1) {
    border-top: 1px solid #ccc;
  }
  .second {
    border-bottom: 1px solid #ccc;
    &:last-child {
      border-bottom: none;
    }
  }
  .el-tag {
    margin: 15px;
  }
}


</style>